package com.koo.modules.sale.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.koo.common.response.R;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.UserUtils;
import com.koo.common.utils.excel.BaseDateConverter;
import com.koo.common.utils.excel.EasyExcelUtil;
import com.koo.modules.sale.entity.SalePayOrderEntity;
import com.koo.modules.sale.service.SalePayOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author charlin
 * @email 136137465@qq.com
 * @date 2023-03-16 16:57:09
 */
@Slf4j
@Api(tags = "")
@RestController
@RequestMapping("sale/salepayorder")
public class SalePayOrderController {

    @Autowired
    private SalePayOrderService salePayOrderService;

    private Long storeId = UserUtils.getStoreId();

    @ApiOperation("获取分页列表")
    @GetMapping("/page")
    @RequiresPermissions("sale:salepayorder:list")
    public R page(@RequestParam Map<String, Object> params){
        PageUtils page = salePayOrderService.queryPage(params);
        return R.ok().put("page", page);
    }

    @ApiOperation("根据参数获取列表")
    @GetMapping("/list")
    @RequiresPermissions("sale:salepayorder:list")
    public R list(@RequestParam Map<String, Object> params){
        List<SalePayOrderEntity> list = salePayOrderService.listByMap(params);
        return R.ok().put("list", list);
    }

    @ApiOperation("获取全部列表")
    @RequestMapping("/listByStoreId")
    @RequiresPermissions("sale:salepayorder:list")
    public R listByStoreId(){
        List<SalePayOrderEntity> list = salePayOrderService.listByStoreId(storeId);
        return R.ok().put("list", list);
    }

    @ApiOperation("根据主键查询信息")
    @GetMapping("/info/{payId}")
    @RequiresPermissions("sale:salepayorder:info")
    public R info(@PathVariable("payId") String payId){
		SalePayOrderEntity salePayOrder = salePayOrderService.getById(payId);
        return R.ok().put("salePayOrder", salePayOrder);
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    @RequiresPermissions("sale:salepayorder:save")
    public R save(@RequestBody SalePayOrderEntity salePayOrder){
		salePayOrderService.save(salePayOrder);
        return R.ok();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @RequiresPermissions("sale:salepayorder:update")
    public R update(@RequestBody SalePayOrderEntity salePayOrder){
		salePayOrderService.updateById(salePayOrder);
        return R.ok();
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete")
    @RequiresPermissions("sale:salepayorder:delete")
    public R delete(@RequestBody Long[] payIds){
		salePayOrderService.removeByIds(Arrays.asList(payIds));
        return R.ok();
    }


    @ApiOperation("导入")
    @RequestMapping("/importData")
    @RequiresPermissions("sale:salepayorder:import")
    @Transactional(rollbackFor = {Exception.class})
    public R importData(@RequestParam("file") MultipartFile file) {
        try {
            // 读取文件流
            EasyExcel.read(
                    file.getInputStream(),
                    SalePayOrderEntity.class,
                    new EasyExcelUtil(salePayOrderService))// 监听器
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet()
                    .registerConverter(new BaseDateConverter.LocalDateConverter())
                    .doRead();
        } catch (IOException e) {
            return R.error("上传失败!");
        }
        return R.ok("上传成功!");
    }

    @ApiOperation("导出")
    @GetMapping("/exportData")
    @RequiresPermissions("sale:salepayorder:export")
    public void exportData(HttpServletResponse response) {
        try {
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = EasyExcelUtil.initExportParams( "报表", response);
            List<SalePayOrderEntity> list = salePayOrderService.list(null);
            EasyExcel.write(response.getOutputStream()
                    , SalePayOrderEntity.class)
                    .registerWriteHandler(horizontalCellStyleStrategy) //加载样式
                    .sheet()
                    .doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
